package com.nervenets.shopping.event;

import com.nervenets.general.redis.RedisSyncLocker;
import com.nervenets.general.utils.SpringContextHolder;
import com.nervenets.shopping.service.GroupBuyOrderService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

import javax.annotation.Resource;

@Slf4j
@AllArgsConstructor
public class GroupBuyOrderSuccessEventListener {
    @Resource
    private final GroupBuyOrderService groupBuyOrderService;
    @Resource
    private final RedisSyncLocker redisSyncLocker;

    @Async
    @Order(0)
    @EventListener(GroupBuyOrderSuccessEvent.class)
    public void handle(GroupBuyOrderSuccessEvent event) throws Exception {
        redisSyncLocker.keyLock("GroupBuyOrderSuccessEventListener", () -> {
            groupBuyOrderService.successHandle();
            SpringContextHolder.publishEvent(new DataOfUserEvent(DataOfUserEvent.Type.refund, 0, 0));
            return null;
        });
    }
}
